﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF6CodeFirstMySQL.Model
{
    public class DataModelInitializer : DropCreateDatabaseIfModelChanges<DataModelContext> // CreateDatabaseIfNotExists<DataModelContext>
    {
        protected override void Seed(DataModelContext context)
        {
            var contracts = new List<Contract>
            {
                new Contract{ BillNo = "PO20150201-001", BillDate=new DateTime(2015,  2,  1), TotalPrice=9876543.21M, Supplier = "Microsoft"},
                new Contract{ BillNo = "PO20141230-088", BillDate=new DateTime(2014, 12, 30), TotalPrice=1234567.89M, Supplier = "Oracle"},
            };
            context.Contracts.AddRange(contracts);
            context.SaveChanges();//可以分次提交，也可以最后一次性提交给数据库

            var deliveries = new List<DeliveryNote>
            {
                new DeliveryNote{ BillNo = string.Format("DN{0:yyyyMMdd}-006", DateTime.Today), TotalPrice=445566M, Contract=contracts.First(), Checker="张三"},
            };
            context.DeliveryNotes.AddRange(deliveries);
            context.SaveChanges();//可以分次提交，也可以最后一次性提交给数据库
        }

    }

}
